define(function (require, exports, module) {

    // 通过 require 引入依赖
    // var $ = require('jquery');
    require('common');
    require('sui');
    var template = require('template');
    dateFormat(template);
    var rows = 20;

    function initData() {
        getChannelData(1, rows);
        getIosData(1, rows);
        getAndroidData(1, rows);
    }


    $(document).on('click', '.check-all-channel', function () {
        if ($('#hasGame').hasClass('active')) {
            return;
        }
        if ($(this).is(":checked")) {
            $('input[name="channel-id"]').prop("checked", true);//全选
            $('#tab-channel tbody tr').addClass('tr-click');
        } else {
            $('input[name="channel-id"]').prop("checked", false);//取消全选
            $('#tab-channel tbody tr').removeClass('tr-click')
        }
    }).on('click', '.query-channel', function () {
        getChannelData(1, rows);
    }).on('click', '.hasGame', function () {
        $('input[name="channel-id"]').prop("checked", false);
        $('input[name="channel-id"]').eq(0).prop("checked", true);
        $('#tab-channel tbody tr').removeClass('tr-click');
        $('#tab-channel tbody tr').eq(0).addClass('tr-click');
        getHasData(1, rows);
    }).on('click', '.channel-id', function () {
        if ($('#hasGame').hasClass('active')) {
            $('input[name="channel-id"]').prop("checked", false);
            $('#tab-channel tbody tr').removeClass('tr-click')
            $(this).find('input').prop("checked", true);
            $(this).addClass('tr-click');
            getAndroidData(1, rows);
            getIosData(1, rows);
            getHasData(1, rows);
        } else {
            getAndroidData(1, rows);
            getIosData(1, rows);
        }
    }).on('click', '.query-ios', function () {
        getIosData(1, rows);
    }).on('click', '.query-android', function () {
        getAndroidData(1, rows);
    }).on('click', '.check-all-ios', function () {
        if ($(this).is(":checked")) {
            $('input[name="add-game-ios"]').prop("checked", true);//全选
            $('#tab-ios tbody tr').addClass('tr-click');
        } else {
            $('input[name="add-game-ios"]').prop("checked", false);//取消全选
            $('#tab-ios tbody tr').removeClass('tr-click')
        }
    }).on('click', '.check-all-android', function () {
        if ($(this).is(":checked")) {
            $('input[name="add-game-android"]').prop("checked", true);//全选
            $('#tab-android tbody tr').addClass('tr-click');
        } else {
            $('input[name="add-game-android"]').prop("checked", false);//取消全选
            $('#tab-android tbody tr').removeClass('tr-click')
        }
    }).on('click', '.check-all-has', function () {
        if ($(this).is(":checked")) {
            $('input[name="add-game-has"]').prop("checked", true);//全选
            $('#tab-has tbody tr').addClass('tr-click');
        } else {
            $('input[name="add-game-has"]').prop("checked", false);//取消全选
            $('#tab-has tbody tr').removeClass('tr-click')
        }
    }).on('click', '.add-ios', function () {
        addIosGame();

    }).on('click', '.add-android', function () {
        addAndroidGame();

    }).on('click', '.query-has', function () {
        getHasData(1, rows);

    }).on('click', '.game-del', function () {
        delGame();
    });

    function delGame() {
        var inputs = $('input[name="channel-id"]');
        var channelIds = new Array();
        $.each(inputs, function (i, item) {
            if ($(item).is(':checked')) {
                channelIds.push(parseInt($(item).val()));
            }
        });
        if (channelIds.length == 0) {
            toastInfo("请选择渠道");
            return;
        }
        var inputs2 = $('input[name="add-game-has"]');
        var iosIds = new Array();
        $.each(inputs2, function (i, item) {
            if ($(item).is(':checked')) {
                iosIds.push(parseInt($(item).val()));
            }
        });
        if (iosIds.length == 0) {
            toastInfo("请选择游戏");
            return;
        }
        $.post('/game/delGame', {
            channelIds: JSON.stringify(channelIds),
            gameIds: JSON.stringify(iosIds)
        }, function (data) {
            switch (data.code) {
                case 0:
                    toastSuccess("删除成功");
                    getAndroidData(1, rows);
                    getIosData(1, rows);
                    break;
                default:
                    toastError('查询数据失败');
            }
        });
    };
    function addIosGame() {
        var inputs = $('input[name="channel-id"]');
        var channelIds = new Array();
        $.each(inputs, function (i, item) {
            if ($(item).is(':checked')) {
                channelIds.push(parseInt($(item).val()));
            }
        });
        if (channelIds.length == 0) {
            toastInfo("请选择渠道");
            return;
        }
        var inputs2 = $('input[name="add-game-ios"]');
        var iosIds = new Array();
        $.each(inputs2, function (i, item) {
            if ($(item).is(':checked')) {
                iosIds.push(parseInt($(item).val()));
            }
        });
        if (iosIds.length == 0) {
            toastInfo("请选择游戏");
            return;
        }
        $.post('/game/addGame', {
            channelIds: JSON.stringify(channelIds),
            gameIds: JSON.stringify(iosIds)
        }, function (data) {
            switch (data.code) {
                case 0:
                    toastSuccess("添加成功");
                    getAndroidData(1, rows);
                    getIosData(1, rows);
                    break;
                default:
                    toastError('查询数据失败');
            }
        });
    };

    function addAndroidGame() {
        var inputs = $('input[name="channel-id"]');
        var channelIds = new Array();
        $.each(inputs, function (i, item) {
            if ($(item).is(':checked')) {
                channelIds.push(parseInt($(item).val()));
            }
        });
        if (channelIds.length == 0) {
            toastInfo("请选择渠道");
            return;
        }
        var inputs2 = $('input[name="add-game-android"]');
        var iosIds = new Array();
        $.each(inputs2, function (i, item) {
            if ($(item).is(':checked')) {
                iosIds.push(parseInt($(item).val()));
            }
        });
        if (iosIds.length == 0) {
            toastInfo("请选择游戏");
            return;
        }
        $.post('/game/addGame', {
            channelIds: JSON.stringify(channelIds),
            gameIds: JSON.stringify(iosIds)
        }, function (data) {
            switch (data.code) {
                case 0:
                    toastSuccess("添加成功");
                    getAndroidData(1, rows);
                    getIosData(1, rows);
                    break;
                default:
                    toastError('查询数据失败');
            }
        });
    }

    function getChannelData(page, rows) {
        var channelName = $.trim($('#channelName').val());
        var data = {
            channelName: channelName
        };
        $.post('/game/listChannel', data, function (data) {
            switch (data.code) {
                case 0:
                    var list = data.data
                    var html = template('tp-channel', {list: list});
                    $('#tab-channel tbody').empty();
                    $('#tab-channel tbody').html(html);
                    break;
                default:
                    toastError('查询数据失败');
            }
        });
    }

    function getIosData(page, rows) {
        var inputs = $('input[name="channel-id"]');
        var channelIds = new Array();
        $.each(inputs, function (i, item) {
            if ($(item).is(':checked')) {
                channelIds.push(parseInt($(item).val()));
            }
        });
        var gameName = $.trim($('#gameName1').val());
        var gameType = $.trim($('#gameType1').val());
        var data = {
            ids: JSON.stringify(channelIds),
            gameName: gameName,
            gameType: gameType,
            channelHasAddFlag: 0,
            page: page,
            rows: rows,
            plateformType: '2'
        };
        $.post('/game/listGame', data, function (data) {
            switch (data.code) {
                case 0:
                    var list = data.data.list;
                    var count = data.data.count;
                    var html = template('tp-ios', {list: list});
                    $('#tab-ios tbody').empty();
                    $('#tab-ios tbody').html(html);
                    $('.ios-pagination').pagination('updateItemsCount', count);
                    break;
                default:
                    toastError('查询数据失败');
            }
        });
    }

    function getAndroidData(page, rows) {
        var inputs = $('input[name="channel-id"]');
        var channelIds = new Array();
        $.each(inputs, function (i, item) {
            if ($(item).is(':checked')) {
                channelIds.push(parseInt($(item).val()));
            }
        });
        var gameName = $.trim($('#gameName2').val());
        var gameType = $.trim($('#gameType2').val());
        var data = {
            ids: JSON.stringify(channelIds),
            gameName: gameName,
            gameType: gameType,
            channelHasAddFlag: 0,
            page: page,
            rows: rows,
            plateformType: '3'
        };
        $.post('/game/listGame', data, function (data) {
            switch (data.code) {
                case 0:
                    var list = data.data.list;
                    var count = data.data.count;
                    var html = template('tp-android', {list: list});
                    $('#tab-android tbody').empty();
                    $('#tab-android tbody').html(html);
                    $('.android-pagination').pagination('updateItemsCount', count);
                    break;
                default:
                    toastError('查询数据失败');
            }
        });
    }

    function getHasData(page, rows) {
        var inputs = $('input[name="channel-id"]');
        var channelIds = new Array();
        $.each(inputs, function (i, item) {
            if ($(item).is(':checked')) {
                channelIds.push(parseInt($(item).val()));
            }
        });
        var gameName = $.trim($('#gameName3').val());
        var gameType = $.trim($('#gameType3').val());
        var data = {
            ids: JSON.stringify(channelIds),
            gameName: gameName,
            gameType: gameType,
            channelHasAddFlag: 1,
            page: page,
            rows: rows
        };
        $.post('/game/listGame', data, function (data) {
            switch (data.code) {
                case 0:
                    var list = data.data.list;
                    var count = data.data.count;
                    var html = template('tp-has', {list: list});
                    $('#tab-has tbody').empty();
                    $('#tab-has tbody').html(html);
                    $('.has-pagination').pagination('updateItemsCount', count);
                    break;
                default:
                    toastError('查询数据失败');
            }
        });
    }

    $('.normal-pagination').pagination({
        itemsCount: 0,
        pageSize: rows,
        showCtrl: true,
        displayPage: 6,
        onSelect: function (num) {
            getNormalData(num, rows)
        }
    });

    $('.delete-pagination').pagination({
        itemsCount: 0,
        pageSize: rows,
        showCtrl: true,
        displayPage: 6,
        onSelect: function (num) {
            getDeleteData(num, rows)
        }
    });

    function dateFormat(template) {
        template.helper('dateFormat', function (date, format) {
            date = new Date(date);
            var map = {
                "M": date.getMonth() + 1, //月份
                "d": date.getDate(), //日
                "h": date.getHours(), //小时
                "m": date.getMinutes(), //分
                "s": date.getSeconds(), //秒
                "q": Math.floor((date.getMonth() + 3) / 3), //季度
                "S": date.getMilliseconds() //毫秒
            };
            format = format.replace(/([yMdhmsqS])+/g, function (all, t) {
                var v = map[t];
                if (v !== undefined) {
                    if (all.length > 1) {
                        v = '0' + v;
                        v = v.substr(v.length - 2);
                    }
                    return v;
                }
                else if (t === 'y') {
                    return (date.getFullYear() + '').substr(4 - all.length);
                }
                return all;
            });
            return format;
        });
    }

    initData();
});